import django_filters
from django.db.models import Q, F
from apps.coffee import models
from django.contrib.auth import get_user_model
from apps.coffee_admin.utils import filter_channel_1, filter_channel_2, filter_channel_3

UserModel = get_user_model()


class OrdersFilter(django_filters.FilterSet):
    sales_type = django_filters.NumberFilter(label="消费类型", help_text="消费类型", method="filter_sales_type")
    # coffee_client_id = django_filters.NumberFilter(help_text="机器id", label="机器id")
    # orderNo = django_filters.NumberFilter(help_text="订单号", label="订单号", lookup_expr="contains")
    start_time = django_filters.DateTimeFilter(help_text="开始时间", label="开始时间", field_name="create_time",
                                               lookup_expr="gte")
    end_time = django_filters.DateTimeFilter(help_text="结束时间", label="结束时间", field_name="create_time",
                                             lookup_expr="lte")
    result = django_filters.ChoiceFilter(choices=models.CoffeeOrder.orderStatus, label="订单状态", help_text="订单状态")
    # goods_id = django_filters.NumberFilter(label="商品id", help_text="商品id")
    # user_channel_id = django_filters.NumberFilter(label="渠道", help_text="渠道", method="filter_user_channel")
    channel_id = django_filters.NumberFilter(label="商家id", help_text="商家id", method="filter_channel_1")
    store_id = django_filters.NumberFilter(label="门店id", help_text="门店id", method="filter_channel_2")
    salesman = django_filters.NumberFilter(label="售货员id", help_text="售货员id", method="filter_channel_3")
    channel_id_coupon = django_filters.NumberFilter(label="商家id", help_text="商家id",
                                                    method="filter_channel_1_coupon")
    store_id_coupon = django_filters.NumberFilter(label="门店id", help_text="门店id", method="filter_channel_2_coupon")
    salesman_coupon = django_filters.NumberFilter(label="售货员id", help_text="售货员id",
                                                  method="filter_channel_3_coupon")
    mobile = django_filters.CharFilter(label="购买人手机号", help_text="购买人手机号", field_name="user__mobile",
                                       lookup_expr="contains")
    # coffee type
    c_type = django_filters.NumberFilter(label="产品价格", help_text="产品价格", field_name="goods__c_type")
    #
    discount_type = django_filters.NumberFilter(label="优惠类型", help_text="优惠类型", method="filter_discount_type")
    goods_name = django_filters.CharFilter(help_text="商品名称", label="商品名称", lookup_expr="contains")
    coupon_store_id = django_filters.NumberFilter(field_name="coupon__channel_id", label="券门店来源",
                                                  help_text="券门店来源")
    coupon_salesman_id = django_filters.NumberFilter(field_name="coupon__channel_id", label="券门店服务员来源",
                                                     help_text="券门店服务员来源")
    user_channel_id = django_filters.NumberFilter(label="渠道", help_text="渠道", field_name="coupon__channel_id")

    def filter_discount_type(self, queryset, name, value):
        if value:
            if value == 0:
                return queryset.filter(cash=F('payment'), coupon__isnull=True)
            elif value == 10:  # 没有券 但是也有优惠 有可能是微信商户配置优惠
                return queryset.filter(cash__gt=F('payment'), coupon__isnull=True)
            else:
                return queryset.filter(coupon__c_type=value)
        return queryset

    def filter_sales_type(self, queryset, name, value):
        if value:
            if value == 3:  # 未知
                return queryset.exclude(coupon__sales_type__in=[1, 2])
            return queryset.filter(coupon__sales_type=value)
        else:
            return queryset

    # def filter_user_channel(self, queryset, name, value):
    #     if value:
    #         return queryset.filter(coupon__channel_id=value)
    #     else:
    #         return queryset

    def filter_channel_1(self, queryset, name, value):
        """
        过滤商家
        """
        queryset = filter_channel_1(queryset, value, coffee_client=True)
        return queryset

    def filter_channel_2(self, queryset, name, value):
        queryset = filter_channel_2(queryset, value, coffee_client=True)
        return queryset

    def filter_channel_3(self, queryset, name, value):
        queryset = filter_channel_3(queryset, value, coffee_client=True)
        return queryset

    def filter_channel_1_coupon(self, queryset, name, value):
        """
        过滤商家
        """
        queryset = filter_channel_1(queryset, value)
        return queryset

    def filter_channel_2_coupon(self, queryset, name, value):
        queryset = filter_channel_2(queryset, value)
        return queryset

    def filter_channel_3_coupon(self, queryset, name, value):
        queryset = filter_channel_3(queryset, value)
        return queryset

    class Meta:
        model = models.CoffeeOrder
        fields = ("start_time", "end_time", "user_channel_id", "channel_id", "store_id",
                  "salesman", "mobile", "c_type", "goods_name", "channel_id_coupon", "store_id_coupon",
                  "salesman_coupon", "sales_type")


class UsersFilter(django_filters.FilterSet):
    channel = django_filters.CharFilter(label="渠道名称", help_text="渠道名称", field_name="coffee_channel__name",
                                        lookup_expr="contains")
    username = django_filters.CharFilter(label="账号", help_text="账号", field_name="username",
                                         lookup_expr="contains")
    mobile = django_filters.CharFilter(label="手机号", help_text="手机号", field_name="mobile",
                                       lookup_expr="contains")

    class Meta:
        model = UserModel
        fields = ("mobile", "username", "channel")
